package com.neuedu.oasystem.view;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;


import com.neuedu.oasystem.entity.Assets;
import com.neuedu.oasystem.service.AssetsService;
import com.neuedu.oasystem.service.impl.AssetsServiceImpl;

public class AssetsView {
	private Scanner scanner;
	private AssetsService assetsService;
	public void init() {
		System.out.println("========================================");
		System.out.println("               资产管理                                                   ");
		System.out.println("========================================");
		System.out.println("1.初始化固定资产                 ");
		System.out.println("2.查询所有固定资产             ");
		System.out.println("3.新增固定资产                    ");
		System.out.println("4.删除固定资产                    ");
		System.out.println("5.采购计划管理                   ");
		System.out.println("6.返回上一级                        ");
		System.out.println("========================================");
	}
	public void work() {
		scanner  = new Scanner(System.in);
		assetsService = new AssetsServiceImpl();
		
		boolean running = true;
		while (running) {
			init();
			System.out.println("请选择：");
			String choose =scanner.nextLine();
			switch (choose) {
			//调用初始化固定资产
			case "1":
				System.out.println("=============初始化固定资产=============");
				boolean s = addAssets();
				if (s) {
					while (s) {
						s = addAssets();
						if (!s) {
							break;
						}
					}
				}
				break;
			case "2":
				System.out.println("=============查询所有固定资产=============");
				selectAssets();
				break;
			case "3":
				
				System.out.println("==============新增固定资产==============");
				addAssets();
				break;
			case "4":
				System.out.println("==============删除固定资产==============");
				deleteAssets();
				break;
			case "5":
				System.out.println("==============采购计划管理==============");
				BuyPlanView buyPlanView =new BuyPlanView();
				buyPlanView.work();
				break;
			case "6":
				System.out.println("===============返回上一级==============");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}
		
	}
	private boolean addAssets() {
		boolean format = false;
		SimpleDateFormat sFormat =new SimpleDateFormat("yyyy-MM-dd");
		scanner = new Scanner(System.in);
		Date purchaseDate = null;
		System.out.println("请输入需要增加的固定资产的资产编号：      ");
		int assetsno = scanner.nextInt();
		System.out.println("请输入需要增加的固定资产的员工编号：      ");
		int empno = scanner.nextInt();
		System.out.println("请输入需要增加的固定资产的资产名称：      ");
		String assetnameString = scanner.next();
		System.out.println("请输入需要增加的固定资产的资产数量：      ");
		int assetnumber = scanner.nextInt();
		System.out.println("请输入需要增加的固定资产的资产价格：      ");
		double assetsprice = scanner.nextDouble();
		System.out.println("请输入需要增加的固定资产的采购人：      ");
		String purchasename = scanner.next();
		System.out.println("请输入需要增加的固定资产的审核人：      ");
		String verifier = scanner.next();
		System.out.println("请输入需要增加的固定资产的采购日期：      ");
		try {
			purchaseDate = sFormat.parse(scanner.next());
		} catch (ParseException e) {
			System.out.println("日期格式错误！");
			format = true;
			return true;
			
		}
		System.out.println("请输入需要增加的固定资产的折旧：      ");
		double depreciation = scanner.nextDouble();
		Assets assets = new Assets(assetsno,empno,assetnameString,assetnumber,assetsprice,purchasename,verifier,purchaseDate,depreciation);
		boolean res = assetsService.add(assets);
		if (res) {
			System.out.println("新增资产成功！");
		}else {
			System.out.println("新增资产失败！");
		}
		return format;
	}
	private void selectAssets() {
		System.out.println("资产编号\t员工编号\t资产名称\t资产数量\t资产价格\t采购人\t审核人\t购买日期\t折旧\t");
		List<Assets> list = assetsService.findAll();
		for (Assets assets : list) {
			System.out.println(assets.getAssetsNo()+"\t"+assets.getEmpId()+"\t"+assets.getAssetsName()+"\t"+assets.getAssetsNumber()+"\t"+assets.getAssetsPrice()
			+"\t"+assets.getPurchasingAgent()+"\t"+assets.getVerifier()+"\t"+assets.getPurchaseDate()+"\t"+assets.getDepreciation());
		}
	}
	
	private void deleteAssets() {
		System.out.println("请输入需要删除的资产编号！");
		int assetsId = scanner.nextInt();
		boolean re = assetsService.deleteById(assetsId);
		if (re) {
			System.out.println("删除成功！");
		}
	}
}
